#include "user_extern.h"

static uint8_t run(void);
static void win_create(void);
static void win_display(void);
static void win_destory(void);
static void update_screen(void);
static uint8_t button_select(void *arg);
static uint8_t button_enter(void *arg);
static uint8_t button_exit(void *arg);
static uint8_t get_id(void);

static Windows window = {
    .id = WIN_UI_DESKTOP,
    .fa_id = WIN_UI_DESKTOP,
    .son_id = {0},
    .name = "desktop",
    .win_status = WINDOWS_STATUS_INIT,
    .run = run,
    .update_screen = update_screen,
    .Dselect = button_select,
    .enter = button_enter,
    .exit = button_exit,
};

static void win_create(void)
{
    return;
}

static void win_destory(void)
{
    window.win_status = WINDOWS_STATUS_INIT;
}

/*********************************************************************
 *  名称： win_display
 *  功能： digimon 显示时间界面
 *  参数： void
 *  返回值：void
 ********************************************************************/
static void win_display(void)
{
    struct tm timeinfo;
    char strftime_buf[64];
    int year = 0;
    int day = 0;
    int hour = 0;
    int min = 0;
    int sec = 0;
    char week[4] = {0};
    char month[4] = {0};

    uint8_t TimestartX = 8;           //时分秒
    uint8_t TimestartY = (8 * 3) - 4; //

    localtime_r(&mtime.g_timestamp, &timeinfo);
    strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
    sscanf(strftime_buf, "%s %s %d %02d:%02d:%02d %d", week, month, &day, &hour, &min, &sec, &year);
    oled.show_string(3, 0, T(week), STR_SIZE_16, 1);
    oled.show_number(46, 0, day, 2, STR_SIZE_16, 1);
    oled.show_string(66, 0, T(month), STR_SIZE_16, 1);
    oled.show_number(93, 0, year, 4, STR_SIZE_16, 1);

    oled.show_number(TimestartX, TimestartY, hour, 2, STR_SIZE_32, 1);
    oled.show_string(TimestartX + 30, TimestartY, T(":"), STR_SIZE_32, 1);
    oled.show_number(TimestartX + 40, TimestartY, min, 2, STR_SIZE_32, 1);
    oled.show_string(TimestartX + 70, TimestartY, T(":"), STR_SIZE_32, 1);
    oled.show_number(TimestartX + 80, TimestartY, sec, 2, STR_SIZE_32, 1);

    /* wifi图标 */
    if(Dsystem.sys_wifi_enable || wifi.wifi_status == SYS_WIFI_STATUS_CONNECTED)
    {
        oled.show_string(3, 48, T("w"), STR_SIZE_16, 1);
    }
    /* 闹钟图标 */
    if(Dsystem.sys_alarm_en)
    {
        oled.show_string(10, 48, T("A"), STR_SIZE_16, 1);
    }
}

static void update_screen(void)
{
    oled.refresh_all(1);
}

static uint8_t button_select(void *arg)
{
    /* setting */
    Dsystem.windows_cur_id = WIN_UI_SETTING;
    //win_destory();
    return 0;
}

static uint8_t button_enter(void *arg)
{
    /* digimon */
    Dsystem.windows_cur_id = WIN_UI_DIGIMON;
    window.win_status = WINDOWS_STATUS_DESTORY;
    //win_destory();
    return 0;
}

static uint8_t button_exit(void *arg)
{
    /* weather */
    Dsystem.windows_cur_id = WIN_UI_WEATHER;
    //win_destory();
    return 0;
}

/* **************************************************************************************** */
static uint8_t run(void)
{
    switch (window.win_status)
    {
    case WINDOWS_STATUS_NONE:
        return 0;
        break;
    case WINDOWS_STATUS_INIT:
        win_create();
        win_display();
        window.win_status = WINDOWS_STATUS_DIS_ONLY;
        break;
    case WINDOWS_STATUS_DIS_ONLY:
        win_display();
        break;
    case WINDOWS_STATUS_DESTORY:
        win_destory();
        break;
    }
    return window.win_status;
}

static uint8_t get_id(void)
{
    return window.id;
}

void win00_desktop_init(void)
{
    windows[get_id()] = window;
}
